package com.oa.modules.approvalsec1.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.common.utils.AppUtils;
import com.oa.modules.approvalsec1.entity.InstitutionsEntity;
import com.oa.modules.approvalsec1.entity.JointsEntity;
import com.oa.modules.approvalsec1.service.InstitutionsService;
import com.oa.modules.approvalsec1.service.JointsService;
import com.oa.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;



/**
 * 联合体
 *
 * @author Hankaicheng
 * @email 785297226@qq.com
 * @date 2022-05-05 15:23:15
 */
@RestController
@RequestMapping("joints")
@Api(tags = "联合体相关接口")
public class JointsController extends AbstractController {
    @Autowired
    private JointsService jointsService;
    @Autowired
    private InstitutionsService institutionsService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public R list(@RequestParam Map<String, Object> params){
        List<JointsEntity> list = jointsService.list();
        return R.ok().put("data", list);
    }

    @ApiOperation("机构树状列表")
    @GetMapping("/treeList")
    public R treeList(){
        List<JointsEntity> jointsList = jointsService.list();
        jointsList.forEach(item->{
            List<InstitutionsEntity> joints = institutionsService.list(new QueryWrapper<InstitutionsEntity>().eq("joint", item.getId()));
            item.setChildren(joints);
        });
        return R.ok().put("data",jointsList);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息")
    @RequiresPermissions("generator:joints:info")
    public R info(@PathVariable("id") Integer id){
		JointsEntity joints = jointsService.getById(id);
        return R.ok().put("joints", joints);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    @RequiresPermissions("generator:joints:save")
    public R save(@RequestBody JointsEntity joints){
		jointsService.save(joints);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    @RequiresPermissions("generator:joints:update")
    public R update(@RequestBody JointsEntity joints){
		jointsService.updateById(joints);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    @RequiresPermissions("generator:joints:delete")
    public R delete(@RequestBody Integer[] ids){
		jointsService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
